﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Zor_Bll;
using Zor_Modes;
using Zor_Tools;
using Zor_Web.Models;
using Zor_Web.Models.Systems;


namespace Zor_Web.Controllers
{
    [Authorization]
    public class SystemsController : Controller
    {
        //
        // GET: /Systems/
        #region - 用户管理控制器 -
        /// <summary>
        /// 查询所有部门
        /// create zhl by 17-3-26
        /// </summary>
        /// <returns></returns>
        ///      
        public ActionResult SelectDepartmentTree()
        {
            List<V_Department> listNode = Sys_Users_Bll<V_Department>.SelectDepartmentTree();
            return Content(listNode.ToJson());
        }
        /// <summary>
        /// 查询所有权限ID和权限名称
        /// create zhl by 2017.3.24
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectRolse()
        {
            List<Sys_Roles> listNode = Sys_Users_Bll<Sys_Roles>.SelectRols();
            return Content(listNode.ToJson());
        }
        /// <summary>
        /// 用户列表显示
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="sqlWhere"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public ActionResult Users(int? index = 1, string where = null)
        {
            int totalCount = 0;
            int pageIndex = index ?? 1;
            PagedList<V_Users> userPage = Sys_Users_Bll<V_Users>.GetPageList(pageIndex, out totalCount,where).AsQueryable().ToPagedList(0, 10) as PagedList<V_Users>;
            userPage.TotalItemCount = totalCount;
            userPage.CurrentPageIndex = (int)(index ?? 1);
            V_Users vr = new V_Users();
            vr.PageData = userPage;
            return View(vr);
        }
        /// <summary>
        /// 删除用户信息
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="User_Id">选中用户的id</param>
        /// <returns></returns>
        public ActionResult UserDeleteSysUser(int[] id)
        {
            BaseAnswer bar = Sys_Users_Bll<dynamic>.Deletesysusers(id);
            return Content(bar.ToJson());
        }

        /// <summary>
        /// 新增/修改用户
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="sysuser">用户对象</param>
        /// <returns>执行结果消息</returns>
        [HttpPost]
        public ActionResult AddAlterSysUser(Sys_Users sysuser)
        {
            BaseAnswer bar = new BaseAnswer();
            if (sysuser.User_Id > 0)
            {
                bar = Sys_Users_Bll<dynamic>.Updatesysusers(sysuser);   //修改
            }
            else 
            {
                bar = Sys_Users_Bll<dynamic>.Insertsysusers(sysuser);   //新增
            }


            return Content(bar.ToJson());
        }

        /// <summary>
        /// 设置账户为可用
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UnlockUser(int[] id)
        {
            BaseAnswer bar = Sys_Users_Bll<dynamic>.UnlockUser(id);
            return Content(bar.ToJson());
        }
        #endregion

        #region - 菜单管理 -
        /// <summary>
        /// 查询所有菜单
        /// create zhl by 17-3-26
        /// </summary>
        /// <returns></returns>
        ///      
        public ActionResult SelectNavTree()
        {
            List<V_Navigations> listNode = Sys_Navigations_Bll<V_Navigations>.SelectNavTree();
            return Content(listNode.ToJson());
        }
        /// <summary>
        /// 菜单列表
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="index">当前页数</param>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public ActionResult Navigations(int? index = 1, string where = null)
        {
            int totalCount = 0;
            int pageIndex = index ?? 1;
            PagedList<V_Navigations> Navigations = Sys_Navigations_Bll<V_Navigations>.GetPageLists(pageIndex, out totalCount).AsQueryable().ToPagedList(0, 10) as PagedList<V_Navigations>;
            Navigations.TotalItemCount = totalCount;
            Navigations.CurrentPageIndex = (int)(index ?? 1);

            V_Navigations vn = new V_Navigations();
            vn.PageData = Navigations;

            return View(vn);
        }

        /// <summary>
        /// 删除菜单信息
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="Nav_Id">选中菜单的id</param>
        /// <returns></returns>
        public ActionResult DeleteNavigations(int[] id)
        {
            BaseAnswer bar = Sys_Navigations_Bll<dynamic>.DeleteNavigations(id);
            return Content(bar.ToJson());
        }

        /// <summary>
        /// 新增/修改菜单
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="navigation">菜单对象</param>
        /// <returns>执行结果消息</returns>
        [HttpPost]
        public ActionResult AddAlterNavigations(Sys_Navigations sys_navigations)
        {

            BaseAnswer bar = new BaseAnswer();
            if (sys_navigations.Nav_Id > 0)
                bar = Sys_Navigations_Bll<dynamic>.UpdateNavigations(sys_navigations);   //修改
            else
                bar = Sys_Navigations_Bll<dynamic>.InsertNavigations(sys_navigations);   //新增

            return Content(bar.ToJson());

        }

        #endregion

        #region - 角色管理 -


        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public ActionResult Roles(int? index = 1, string where = null)
        {
            int totalCount = 0;
            int pageIndex = index ?? 1;
            PagedList<Sys_Roles> rsPager = Sys_Roles_Bll<Sys_Roles>.GetPageList(pageIndex, out totalCount).AsQueryable().ToPagedList(0, 10) as PagedList<Sys_Roles>;
            rsPager.TotalItemCount = totalCount;
            rsPager.CurrentPageIndex = (int)(index ?? 1);

            V_Roles vr = new V_Roles();
            vr.PageData = rsPager;
            vr.lstMenu = BaseClass.navigations;

            return View(vr);
        }

        /// <summary>
        /// 新增/修改角色
        /// </summary>
        /// <param name="roles">角色对象</param>
        /// <returns>执行结果消息</returns>
        [HttpPost]
        public ActionResult AddRoles(Sys_Roles roles)
        {
            BaseAnswer bar = new BaseAnswer();
            if (roles.Roles_Id > 0)
                bar = Sys_Roles_Bll<dynamic>.UpdateRoles(roles);   //修改
            else
                bar = Sys_Roles_Bll<dynamic>.InsertRoles(roles);   //新增

            return Content(bar.ToJson());

        }


        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="Roles_Id">选中角色的id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteRoles(int[] id)
        {
            BaseAnswer bar = Sys_Roles_Bll<dynamic>.DeleteRoles(id);
            return Content(bar.ToJson());
        }


        /// <summary>
        /// 获取角色菜单
        /// </summary>
        /// <param name="rolesId"></param>
        /// <returns></returns>
        public ActionResult LoadNavigationsMenu(int rolesId)
        {
            List<Sys_Jurisdiction> lstMenu = Sys_Roles_Bll<Sys_Jurisdiction>.GetMenuList(rolesId);
            return Content(lstMenu.ToJson());
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <returns></returns>
        public ActionResult AssignPermissions(string rolesId, string[] menuId)
        {
            BaseAnswer bar = Sys_Roles_Bll<dynamic>.Allocation(rolesId, menuId);
            return Content(bar.ToJson());
        }

        #endregion

        #region - 部门管理控制器 -
        /// <summary>
        /// 视图
        /// create liuqi by 2017.3.24
        /// </summary>
        /// <returns></returns>
        public ActionResult Department()
        {
            return View();
        }

        /// <summary>
        /// 显示部门
        /// create liuqi by 2017.3.24
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadDepartment()
        {
            List<V_Department> department = Sys_Department_Bll<V_Department>.GetDepartmentList();
            return Content(department.ToJson());
        }

        /// <summary>
        /// 添加/编辑部门
        /// create liuqi by 2017.3.29
        /// </summary>
        /// <param name="Department">部门</param>
        /// <returns></returns>
        public ActionResult AddAlterDepartment(Sys_Department Department, string oldName, bool isAddAlter)
        {
            BaseAnswer bar = new BaseAnswer();
            if (!isAddAlter && Department.Dt_Id > 0)
                bar = Sys_Department_Bll<dynamic>.AlterDepartment(Department, oldName);   //修改
            else
                bar = Sys_Department_Bll<dynamic>.InsertDepartment(Department);           //新增
            return Content(bar.ToJson());
        }

        /// <summary>
        /// 删除部门
        /// create liuqi by 2017.3.24
        /// </summary>
        /// <param name="id">部门id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteDepartment(string id)
        {
            BaseAnswer bar = Sys_Department_Bll<dynamic>.DeleteDepartment(id);
            return Content(bar.ToJson());
        }

        /// <summary>
        /// 查询要删除的部门是否拥有下级部门
        /// create liuqi by 2017.3.24
        /// </summary>
        /// <returns>返回消息json</returns>
        [HttpPost]
        public ActionResult SelectDepartment(string id)
        {
            BaseAnswer bar = Sys_Department_Bll<dynamic>.SelectDepartment(id);
            return Content(bar.ToJson());
        }

        /// <summary>
        /// 显示类别不为4的部门
        /// create liuqi by 2017.3.24
        /// </summary>
        /// <returns>返回表json</returns>
        [HttpPost]
        public ActionResult SelectDtparentid()
        {
            List<Sys_Department> lstDtparentid = Sys_Department_Bll<Sys_Department>.SelectDtparentid();
            return Content(lstDtparentid.ToJson());
        }
        #endregion

    }
}
